package com.cz.nongyetongv5.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.EntList;
import com.cz.nongyetongv5.custom.dialog.CustomMyDialog;
import com.cz.nongyetongv5.fragment.EntListFragment;
import com.ronri.viewpagerindicator.TitlePageIndicator;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class EntListActivity extends FragmentActivity implements View.OnClickListener {
    private RelativeLayout btnBack;
    private TabAdapter adapter;
    private ViewPager viewPager;
    private TextView tv_right;
    private List<Fragment> mFragments;
    private TitlePageIndicator tpiIndicator;
    private LinearLayout ll_bottom;
    private TextView tv_pass;
    private TextView tv_refuse;
    private int addIndex = 1;
    private int modifyIndex = 1;
    private int confirmIndex = 1;
    private int cancelIndex = 1;


    private String add_user;
    //    private String regions;
    private String region_code;


    public int getAddIndex() {
        return addIndex;
    }

    public void setAddIndex(int addIndex) {
        this.addIndex = addIndex;
    }

    public int getModifyIndex() {
        return modifyIndex;
    }

    public void setModifyIndex(int modifyIndex) {
        this.modifyIndex = modifyIndex;
    }

    public int getConfirmIndex() {
        return confirmIndex;
    }

    public void setConfirmIndex(int confirmIndex) {
        this.confirmIndex = confirmIndex;
    }

    public int getCancelIndex() {
        return cancelIndex;
    }

    public void setCancelIndex(int cancelIndex) {
        this.cancelIndex = cancelIndex;
    }

    // 分页视图标题
    private List<String> tabTitles;

    // 列表控件
    private Context mContext = null;
    private String ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ent_list);
        add_user = getIntent().getStringExtra("add_user");
        //        regions = getIntent().getStringExtra("regions");
        region_code = getIntent().getStringExtra("region_code");
        mFragments = new ArrayList<>();
        tabTitles = new ArrayList<>();
        tabTitles.add("新增");
        tabTitles.add("修改");
        tabTitles.add("确认");
        tabTitles.add("注销");
        mFragments.add(EntListFragment.newInstance(1, add_user, "1", region_code));
        mFragments.add(EntListFragment.newInstance(2, add_user, "2", region_code));
        mFragments.add(EntListFragment.newInstance(3, add_user, "4", region_code));
        mFragments.add(EntListFragment.newInstance(4, add_user, "3", region_code));
        ll_bottom = findViewById(R.id.ll_bottom);
        tv_pass = findViewById(R.id.tv_pass);
        tv_refuse = findViewById(R.id.tv_refuse);
        tv_pass.setOnClickListener(this);
        tv_refuse.setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.tv_right);
        tv_right.setText("审核");
        tv_right.setOnClickListener(this);
        btnBack = (RelativeLayout) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        adapter = new TabAdapter(getSupportFragmentManager(), mFragments, tabTitles);
        viewPager = (ViewPager) findViewById(R.id.vpNewListView);
        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(adapter);
        tpiIndicator = (TitlePageIndicator) findViewById(R.id.tpiIndicator);
        tpiIndicator.setViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                EntListFragment fragment = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                if (viewPager.getCurrentItem() == 0) {
                    if (addIndex == 1) {
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);

                    } else if (addIndex == 2) {
                        tv_right.setText("全选");
                        ll_bottom.setVisibility(View.VISIBLE);

                    } else if (addIndex == 3) {
                        tv_right.setText("全不选");
                        ll_bottom.setVisibility(View.VISIBLE);
                    }
                } else if (viewPager.getCurrentItem() == 1) {
                    if (modifyIndex == 1) {
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);

                    } else if (modifyIndex == 2) {
                        tv_right.setText("全选");
                        ll_bottom.setVisibility(View.VISIBLE);
                    } else if (modifyIndex == 3) {

                        tv_right.setText("全不选");
                        ll_bottom.setVisibility(View.VISIBLE);
                    }

                } else if (viewPager.getCurrentItem() == 2) {
                    if (confirmIndex == 1) {
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);

                    } else if (confirmIndex == 2) {
                        tv_right.setText("全选");
                        ll_bottom.setVisibility(View.VISIBLE);

                    } else if (confirmIndex == 3) {
                        tv_right.setText("全不选");
                        ll_bottom.setVisibility(View.VISIBLE);
                    }
                } else if (viewPager.getCurrentItem() == 3) {
                    if (cancelIndex == 1) {
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);

                    } else if (cancelIndex == 2) {
                        tv_right.setText("全选");
                        ll_bottom.setVisibility(View.VISIBLE);

                    } else if (cancelIndex == 3) {
                        tv_right.setText("全不选");
                        ll_bottom.setVisibility(View.VISIBLE);

                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public void onClick(View view) {
        ids = "";
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.tv_pass:
                EntListFragment fragment1 = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                List<EntList> lists = fragment1.getSelected();
                if (lists.size() <= 0) {
                    Toast.makeText(EntListActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < lists.size(); i++) {
                        if (i == 0) {
                            ids = String.valueOf(lists.get(i).getE_id());
                        } else {
                            ids = ids + "," + lists.get(i).getE_id();
                        }
                    }
                    CustomMyDialog.Builder builder = new CustomMyDialog.Builder(EntListActivity.this);
                    builder.setMessage("确认提交？");
                    builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            postCheckEnt("1", ids, "", add_user, region_code);
                        }
                    });
                    builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.createNormalDialog().show();


                    //                EntListFragment fragment1 = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                    //                List<EntList> lists = fragment1.getSelected();
                    //                if (lists.size() <= 0) {
                    //                    Toast.makeText(EntListActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                    //                } else {
                    //                    for (int i = 0; i < lists.size(); i++) {
                    //                        if (i == 0) {
                    //                            ids = lists.get(i).getEnt_id();
                    //                        } else {
                    //                            ids = ids + "," + lists.get(i).getEnt_id();
                    //                        }
                    //                    }
                    //                    Intent intent = new Intent(EntListActivity.this, DialogActivity.class);
                    //                    intent.putExtra("add_user", add_user);
                    //                    intent.putExtra("region_code", region_code);
                    //                    intent.putExtra("ids", ids);
                    //                    intent.putExtra("type", "1");
                    //                    intent.putExtra("model", "ent");
                    //                    startActivityForResult(intent, 9002);
                }

                break;
            case R.id.tv_refuse:
                EntListFragment fragment2 = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                List<EntList> lists2 = fragment2.getSelected();
                if (lists2.size() <= 0) {
                    Toast.makeText(EntListActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < lists2.size(); i++) {
                        if (i == 0) {
                            ids = String.valueOf(lists2.get(i).getE_id());
                        } else {
                            ids = ids + "," + lists2.get(i).getE_id();
                        }
                    }
                    Intent intent2 = new Intent(EntListActivity.this, DialogActivity.class);
                    intent2.putExtra("add_user", add_user);
                    intent2.putExtra("region_code", region_code);
                    intent2.putExtra("ids", ids);
                    intent2.putExtra("type", "2");
                    intent2.putExtra("model", "ent");
                    startActivityForResult(intent2, 9002);
                }

                break;
            case R.id.tv_right:
                EntListFragment fragment = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                if (fragment.getSize() <= 0) {
                    Toast.makeText(EntListActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                } else {
                    if (viewPager.getCurrentItem() == 0) {
                        if (addIndex == 1) {
                            tv_right.setText("全选");
                            fragment.setNotify(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            addIndex = 2;
                        } else if (addIndex == 2) {
                            tv_right.setText("全不选");
                            fragment.setSelect(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            addIndex = 3;
                        } else if (addIndex == 3) {
                            addIndex = 2;
                            tv_right.setText("全选");
                            ll_bottom.setVisibility(View.VISIBLE);
                            fragment.setSelect(false);
                        }
                    } else if (viewPager.getCurrentItem() == 1) {
                        if (modifyIndex == 1) {
                            tv_right.setText("全选");
                            fragment.setNotify(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            modifyIndex = 2;
                        } else if (modifyIndex == 2) {
                            tv_right.setText("全不选");
                            fragment.setSelect(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            modifyIndex = 3;
                        } else if (modifyIndex == 3) {
                            modifyIndex = 2;
                            tv_right.setText("全选");
                            ll_bottom.setVisibility(View.VISIBLE);
                            fragment.setSelect(false);
                        }

                    } else if (viewPager.getCurrentItem() == 2) {
                        if (confirmIndex == 1) {
                            tv_right.setText("全选");
                            fragment.setNotify(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            confirmIndex = 2;
                        } else if (confirmIndex == 2) {
                            tv_right.setText("全不选");
                            fragment.setSelect(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            confirmIndex = 3;
                        } else if (confirmIndex == 3) {
                            confirmIndex = 2;
                            tv_right.setText("全选");
                            fragment.setSelect(false);
                            ll_bottom.setVisibility(View.VISIBLE);
                        }
                    } else if (viewPager.getCurrentItem() == 3) {
                        if (cancelIndex == 1) {
                            tv_right.setText("全选");
                            fragment.setNotify(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            cancelIndex = 2;
                        } else if (cancelIndex == 2) {
                            tv_right.setText("全不选");
                            fragment.setSelect(true);
                            ll_bottom.setVisibility(View.VISIBLE);
                            cancelIndex = 3;
                        } else if (cancelIndex == 3) {
                            cancelIndex = 2;
                            tv_right.setText("全选");
                            fragment.setSelect(false);
                            ll_bottom.setVisibility(View.VISIBLE);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9002:
                    //                    finish();
                    EntListFragment fragment = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                    if (viewPager.getCurrentItem() == 0) {
                        addIndex = 1;
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);
                        fragment.setUpdatet(false);
                    } else if (viewPager.getCurrentItem() == 1) {
                        modifyIndex = 1;
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);
                        fragment.setUpdatet(false);
                    } else if (viewPager.getCurrentItem() == 2) {
                        confirmIndex = 1;
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);
                        fragment.setUpdatet(false);
                    } else if (viewPager.getCurrentItem() == 3) {
                        cancelIndex = 1;
                        tv_right.setText("审核");
                        ll_bottom.setVisibility(View.GONE);
                        fragment.setUpdatet(false);
                    }
                    break;
                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 适配器代码
     */
    class TabAdapter extends FragmentStatePagerAdapter {
        public List<Fragment> list;
        private List<String> titles;

        public TabAdapter(FragmentManager fm, List<Fragment> list, List<String> titles) {
            super(fm);
            this.list = list;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            //            EntListFragment fragment = EntListFragment.newInstance();
            //            Bundle args = new Bundle();
            //            args.putInt("type", 0);
            //            args.putString("args", szTabTitle[position]);
            //            args.putInt("single",position+1);
            //            fragment.setArguments(args);
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }

    void postCheckEnt(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.newUrl + "ent/checkEnt.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", "")
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        //                        Toast.makeText(EntListActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(EntListActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                //                                setResult(Activity.RESULT_OK);
                                //                                finish();
                                EntListFragment fragment = (EntListFragment) mFragments.get(viewPager.getCurrentItem());
                                if (viewPager.getCurrentItem() == 0) {
                                    addIndex = 1;
                                    tv_right.setText("审核");
                                    ll_bottom.setVisibility(View.GONE);
                                    fragment.setUpdatet(false);
                                } else if (viewPager.getCurrentItem() == 1) {
                                    modifyIndex = 1;
                                    tv_right.setText("审核");
                                    ll_bottom.setVisibility(View.GONE);
                                    fragment.setUpdatet(false);
                                } else if (viewPager.getCurrentItem() == 2) {
                                    confirmIndex = 1;
                                    tv_right.setText("审核");
                                    ll_bottom.setVisibility(View.GONE);
                                    fragment.setUpdatet(false);
                                } else if (viewPager.getCurrentItem() == 3) {
                                    cancelIndex = 1;
                                    tv_right.setText("审核");
                                    ll_bottom.setVisibility(View.GONE);
                                    fragment.setUpdatet(false);
                                }
                            } else {
                                //                                Toast.makeText(EntListActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
